<?php
class Morderout extends CI_Model {
    protected $_table;
    public function __construct() {
        parent::__construct();
        $this->_table="order_out";
    }
    
    public function listAll($amount, $start) {
        $query=$this->db->query("select o.*, u.u_username, s.status_name from order_out as o
                                inner join users as u on u.u_id = o.u_id
                                inner join status as s on s.status_id = o.status_id
                                limit $start, $amount");
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAll() {
        return $this->db->count_all($this->_table);
    }
    
    public function search($user, $datefrom, $dateto, $pricefrom, $priceto, $status, $amount, $start) {
        $u=$d=$p=$s="'1=1'";
        if($user!="") $u="u.u_username like '%".$user."%'";
        if($datefrom!="" and $dateto!="") $d="oo_date between '".$datefrom."' and '".$dateto."'";
        if($pricefrom!="" and $priceto!="") $p="oo_price between $pricefrom and $priceto";
        if($status!=0) $s="o.status_id = $status";
        $str="select o.*, u.u_username, s.status_name from order_out as o
                inner join users as u on u.u_id = o.u_id
                inner join status as s on s.status_id = o.status_id
                where $u and $d and $p and $s
                limit $start, $amount";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->result_array(); else return false;
    }
    
    public function countAllResults($user, $datefrom, $dateto, $pricefrom, $priceto, $status) {
        $u=$d=$p=$s="'1=1'";
        if($user!="") $u="u.u_username like '%".$user."%'";
        if($datefrom!="" and $dateto!="") $d="oo_date between '".$datefrom."' and '".$dateto."'";
        if($pricefrom!="" and $priceto!="") $p="oo_price between $pricefrom and $priceto";
        if($status!=0) $s="o.status_id = $status";
        $str="select count(*) as 'count' from order_out as o
                inner join users as u on u.u_id = o.u_id
                inner join status as s on s.status_id = o.status_id
                where $u and $d and $p and $s";
        $query=$this->db->query($str);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function getById($id) {
        $this->db->where("oo_id", $id);
        $query=$this->db->get($this->_table);
        if($query->num_rows()>0) return $query->row_array(); else return false;
    }
    
    public function update($data, $id) {
        $this->db->where("oo_id", $id);
        $this->db->update($this->_table, $data);
    }
    
    public function del($id) {
        $this->db->where("oo_id", $id);
        $this->db->delete($this->_table);
    }
}
?>
